package com.css.fxfzfxqh.modules.vulnerabilityData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.vulnerabilityData.entity.BuildingIndividualVulnerability;
import com.css.fxfzfxqh.modules.vulnerabilityData.service.VulnerabilityService;
import com.css.fxfzfxqh.modules.vulnerabilityData.vo.MatrixConvertCurveVo;
import com.css.fxfzfxqh.modules.vulnerabilityData.vo.QueryParamsVo;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/07/4
 * @Description: 地震易损性数据(从承灾体易损性移植)
 */
@RestController
@RequestMapping("/vulnerability")
@Slf4j
public class VulnerabilityController {

    @Autowired
    VulnerabilityService vulnerabilityService;

    /**
     * 上传excel
     * @param file
     * @param attachId
     * @param type
     * @param province
     * @param city
     * @param county
     * @return
     */
    @PostMapping("/uploadExcel")
    public RestResponse uploadExcel(@RequestParam("file") MultipartFile file,String attachId, String type, String province, String city,String county) {
        RestResponse response = null;
        try{
            Map<String, Object> map = vulnerabilityService.uploadExcel(file,attachId, type, province, city, county);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            response=RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 上传shp
     * @param attachId
     * @param type
     * @return
     */
    @PostMapping("/uploadShp")
    public RestResponse uploadShp(String attachId, String type) {
        RestResponse response = null;
        try{
            Map<String, Object> map = vulnerabilityService.uploadShp(attachId, type);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            response=RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 分页查询
     * @param queryParamsVo
     * @param request
     * @return
     */
    @GetMapping("/page")
    public RestResponse queryFshBuildings(QueryParamsVo queryParamsVo, HttpServletRequest request) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = vulnerabilityService.page(queryParamsVo,curPage,pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage = "查询失败!";
            response=RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 详情页
     * @param id
     * @param type
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id,String type){
        RestResponse restResponse = null;
        try {
            Map map = vulnerabilityService.findById(id, type);
            restResponse = RestResponse.succeed(map.get("data"));
            restResponse.setMessage("查询成功");
        } catch (Exception e) {
            String errormessage = "查询失败";
            restResponse = RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return restResponse;
    }

    /**
     * 桥梁详情页
     * @param id
     * @param flag
     * @return
     */
    @GetMapping("/bridgeFindById")
    public RestResponse bridgeFindById(String id,String flag){
        RestResponse restResponse = null;
        try {
            Map map = vulnerabilityService.bridgeFindById(id, flag);
            restResponse = RestResponse.succeed(map.get("data"));
            restResponse.setMessage("查询成功");
        } catch (Exception e) {
            String errormessage = "查询失败";
            restResponse = RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return restResponse;
    }


    /**
     * 修改
     * @param map
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody Map<String,Object>map){
        RestResponse restResponse = null;
        try {
            Boolean res = vulnerabilityService.update(map);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("修改成功");
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error("修改失败!",e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 删除
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody Map<String,Object>map){
        RestResponse restResponse = null;
        try {
            Boolean res = vulnerabilityService.delete(map);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("删除成功");
        } catch (Exception e) {
            String errormessage = "删除失败";
            e.printStackTrace();
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 桥梁删除
     * @param map
     * @return
     */
    @PostMapping("/bridgeDelete")
    public RestResponse bridgeDelete(@RequestBody Map<String,Object>map){
        RestResponse restResponse = null;
        try {
            Boolean res = vulnerabilityService.bridgeDelete(map);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("删除成功");
        } catch (Exception e) {
            String errormessage = "删除失败";
            e.printStackTrace();
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @PostMapping("/importExcel")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file){
        RestResponse restResponse=null;
        try {
            vulnerabilityService.importExcel(file);
            restResponse = RestResponse.succeed("导入成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "导入失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分页列表
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=vulnerabilityService.getPage(curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 矩阵详情
     * @param vulnerabilityCode 易损性编号
     * @return
     */
    @GetMapping("/matrixFindByCode")
    public RestResponse matrixFindByCode(String vulnerabilityCode){
        RestResponse restResponse = null;
        try {
            List<BuildingIndividualVulnerability> list = vulnerabilityService.matrixFindByCode(vulnerabilityCode);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        } catch (Exception e) {
            String errormessage = "查询失败";
            restResponse = RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return restResponse;
    }

    /**
     * 修改
     * @param list
     * @return
     */
    @PostMapping("/matrixUpdate")
    public RestResponse matrixUpdate(@RequestBody List<BuildingIndividualVulnerability> list){
        RestResponse restResponse = null;
        try {
            vulnerabilityService.matrixUpdate(list);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("修改成功");
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error("修改失败!",e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 矩阵转曲线查看
     * @param vulnerability
     * @return
     */
    @PostMapping("/matrixConvertCurve")
    public RestResponse matrixConvertCurve(@RequestBody BuildingIndividualVulnerability vulnerability){
        RestResponse restResponse = null;
        try {
            MatrixConvertCurveVo res = vulnerabilityService.matrixConvertCurve(vulnerability);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("查看成功");
        } catch (Exception e) {
            String errormessage = "查看失败";
            log.error("查看失败!",e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
